﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace COMP376A3_9609695
{
    class Sparks : Updates
    {
        public struct Dot
        {
            public float angle, scale;
            public Vector2 position;

            public Dot(float angle, float scale)
            {
                this.angle = angle;
                this.scale = scale;
                position = new Vector2();
            }
        }
        private List<Dot> dots;
        private Vector3 position;
        public float X { get { return position.X; } set { position.X = value; } }
        public float Y { get { return position.Y; } set { position.Y = value; } }
        public float Z { get { return position.Z; } set { position.Z = value; } }
        public float maxSpeed { get; private set; }
        private float maxDist;
        public int totalTime { get; private set; }

        public Sparks(float X, float Y, float Z, int count)
        {
            position = new Vector3(X, Y, Z);
            Random r = new Random();
            dots = new List<Dot>();
            for (int i = 0; i < count; i++)
            {
                float angle = (float)r.NextDouble() * MathHelper.TwoPi;
                Dot newDot = new Dot(angle, (float)r.NextDouble() / 3 + 0.25f);

                dots.Add(newDot);
            }
            maxSpeed = 50f;
            maxDist = 1.5f;
        }

        public void Update(GameTime time)
        {
            int elapsed = time.ElapsedGameTime.Milliseconds;
            totalTime += elapsed;
            int dotCount = dots.Count;

            float dist = ((float)totalTime / 1000) * maxSpeed;
            for (int d = 0; d < dotCount; d++)
            {
                Dot dot = dots.ElementAt(d);
                float dotDist = dist * dot.scale;
                if (dotDist > maxDist / dot.scale)
                {
                    dots.Remove(dot);
                    d--;
                    dotCount--;
                }
            }
        }

        public List<Matrix> getDotMats()
        {
            List<Matrix> dotMats = new List<Matrix>();
            foreach (Dot dot in dots)
            {
                float dist = (float)totalTime / 1000 * maxSpeed * dot.scale;
                Matrix mat = Matrix.CreateScale(dot.scale + 0.4f) * 
                Matrix.CreateTranslation((float)Math.Cos(dot.angle)* dist + X, (float)Math.Sin(dot.angle)*dist/2 + Y, Z + dist);
                dotMats.Add(mat);
            }
            return dotMats;
        }

        public List<Dot> getDots()
        {
            return dots;
        }
    }
}
